PEAR::MDB simple example

chris (2004-05-02 18:03:06)
3415 views
1 replies
I put this demo together for a colleague - it shows how to use PEAR's MDB classes to abstract your database layer and simplify php/database integration. You will have to install the PEAR MDB module first, which should be as simple as typing 'pear install MDB' at your shell prompt.

        /*
        * simple demo for use of PEAR::MDB
        * $Id: mdbtest.php,v 1.1 2004/02/16 21:33:56 christo Exp $
        * ensure PEAR::MDB module installed on host m/c
        */

This requires a very simple db called mdbtest, with the following table:
        CREATE TABLE testtable (
          tt_id int(11) NOT NULL auto_increment,
          tt_number int(11) default NULL,
          PRIMARY KEY  (tt_id)
        ) TYPE=MyISAM;

Also note: if you choose to use sequences, an additional 'seq' table will be created by MDB, because MySQL doesn't natively support sequences like postgres and some other RDBMS's

        // pull in required class libs
        require_once 'MDB.php';

        $user = 'user';
        $pass = 'password';
        $host = 'localhost';
        $db_name = 'mdbtest';
        $db_type = 'mysql'; // this is the bit you'd change for another RDBMS

        /* Build DSN string */
        $dsn = $db_type . "://"
        . $user . ":"
        . $pass . "@"
        . $host . "/"
        . $db_name;
        echo "$dsn";

        /* get a PEAR::MDB object on connect, or an error object or error */
        $db = MDB::connect($dsn);
        /* just check that it's not an error obj */
        if (MDB::isError($db)) {
                die ($db->getMessage());
        }
from here, your template ends.. and then you just get on with it: so here's a demo of using 'sequences' to manage inserts, and then a standard select query
        for($i=0;$inextId('testtable');
                $number=$id*10;  // just for the sake of creating a number :)
                $result=$db->query("insert into testtable (tt_id, tt_number) values($id, $number)");
                if(MDB::isError( $result )){
                        die("your query returned an error: ".$result->getMessage());
                }
        }
        $result = $db->query("select tt_id from testtable");
        echo "";
        if(MDB::isError( $result )){
                die("your query returned an error: ".$result->getMessage());
        }
        while ($row = $db->fetchInto($result)) {
                echo "";
                print_r($row);
        }

        // close conection
        $db->disconnect();


simple huh? If you want to get a simple email reminder whenever there is a new tutorial posted in this channel, just join the channels now and subscribe to this one with a single mouseclick :) I know the screens look crappy at the moment, but that's because I've been focussing on content rather than style. I'll get round to it :)

christo
comment
chris
2008-09-06 21:18:27

don't use it if performance matters to you

DB and MDB performance sucks. I would just use PDO for this stuff nowadays.
reply icon